package uk.co.iceroad.twu.m1;

import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.*;


import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

public class CharacterPrinterTest {


    private ByteArrayOutputStream testSysOut;

    @Before
    public void before() {
        //Modify Sys Out so we can check the printed output is as expected
        this.testSysOut = new ByteArrayOutputStream();
        System.setOut(new PrintStream(testSysOut));
    }

    @Test
    public void testPrintCharacter() {
        CharacterPrinter characterPrinter = new CharacterPrinter('*');

        characterPrinter.printCharacter();

        assertTrue(testSysOut.size() == 1);
        assertTrue(testSysOut.toString().charAt(0) == '*');
    }

    @Test
    public void testPrintLine() {
        CharacterPrinter characterPrinter = new CharacterPrinter('*');
        characterPrinter.printLine(8);
        assertEquals("********\n", testSysOut.toString());
    }

    @Test
    public void testPrintVerticalLine() {
        CharacterPrinter characterPrinter = new CharacterPrinter('*');

        characterPrinter.printVerticalLine(3);

        String[] output = testSysOut.toString().split("\n");

        assertTrue(output.length == 3);
        assertEquals("*", output[0]);
        assertEquals("*", output[1]);
        assertEquals("*", output[2]);
    }

    @Test
    public void testPrintRightTriangle() {
        CharacterPrinter characterPrinter = new CharacterPrinter('*');

        int size = 3;
        characterPrinter.printRightTriangle(size);

        String[] output = testSysOut.toString().split("\n");
        assertTrue(output.length == 3);

        assertEquals("*", output[0]);
        assertEquals("**", output[1]);
        assertEquals("***", output[2]);
    }


    @Test
    public void testPrintIsoscelesTriangle() {
        CharacterPrinter characterPrinter = new CharacterPrinter('*');

        int height = 3;
        characterPrinter.printIsoscelesTriangle(height);

        String[] output = testSysOut.toString().split("\n");
        assertTrue(output.length == 3);

        assertEquals("  *  ", output[0]);
        assertEquals(" *** ", output[1]);
        assertEquals("*****", output[2]);
    }

    @Test
    public void testPrintDiamond() {
        CharacterPrinter characterPrinter = new CharacterPrinter('*');

        int height = 3;
        characterPrinter.printDiamond(height);
        String[] output = testSysOut.toString().split("\n");
        assertTrue(output.length == 5);

        assertEquals("  *  ", output[0]);
        assertEquals(" *** ", output[1]);
        assertEquals("*****", output[2]);
        assertEquals(" *** ", output[3]);
        assertEquals("  *  ", output[4]);
    }


    @Test
    public void testPrintDiamondWithName() {
        CharacterPrinter characterPrinter = new CharacterPrinter('*');

        int height = 3;
        characterPrinter.printDiamondWithName(height, "Rosy");
        String[] output = testSysOut.toString().split("\n");
        assertTrue(output.length == 5);

        assertEquals("  *  ", output[0]);
        assertEquals(" *** ", output[1]);
        assertEquals("Rosy", output[2]);
        assertEquals(" *** ", output[3]);
        assertEquals("  *  ", output[4]);
    }
}
